﻿using System;
using System.Collections.Generic;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Enums;
using Kugar.UI.Web.AdminLTE.Helpers;
using Kugar.UI.Web.AdminLTE.Interfaces;
using Kugar.UI.Web.AdminLTE.PageControls.Helpers.ButtonExtMethod;
using Kugar.UI.Web.AdminLTE.PageControls.SystemComponent;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageCommon
{
    public class PageCommon_ModalWindow:ContainerBase
    {
        private List<ButtonConfigBase> _leftButtons=new List<ButtonConfigBase>(2);
        private string _title = "";
        private string _iconCss = "fa-windows";
        private List<ButtonConfigBase> _rightButtons=new List<ButtonConfigBase>();
        private string _height = "500px";
        private string _width = "600px";
        private bool _shadeClose = true;

        public PageCommon_ModalWindow(ViewContext viewContext) : base(viewContext)
        {
            this.Css("box modalwindow box-info box-solid");
        }

        public PageCommon_ModalWindow Title(string title)
        {
            _title = title;

            return this;
        }

        public PageCommon_ModalWindow IconCss(string icon)
        {
            _iconCss = icon;

            return this;
        }

        public PageCommon_ModalWindow AddLeftButton(Action<ButtonConfigBase> btnFunc)
        {
            var config=new ButtonConfigBase(ViewContext);

            config.Css("btn");

            btnFunc(config);

            _leftButtons.Add(config);

            return this;
        }

        public PageCommon_ModalWindow AddRightButton(Action<ButtonConfigBase> btnFunc)
        {
            var config = new ButtonConfigBase(ViewContext);

            config.Css("btn");

            btnFunc(config);

            _rightButtons.Add(config);

            return this;
        }

        public PageCommon_ModalWindow DisableShadeClose(bool disableShadeClose = true)
        {
            _shadeClose = !disableShadeClose;

            return this;
        }

        public PageCommon_ModalWindow AddSubmitButton(Action<ButtonConfigBase> btnFunc=null)
        {
            //var config = new ButtonConfigBase(ViewContext);

            //config.Css("btn");

            //btnFunc(config);

            //_rightButtons.Add(config);

            return AddRightButton(btn =>
            {
                btn.Text("保存").Color(ColorPaletteEnum.success).Type(ButtonTypeEnum.submit).Css("btn");

                btnFunc?.Invoke(btn);
            });
            
        }

        public PageCommon_ModalWindow ContentHeight(string height)
        {
            _height = height;

            return this;
        }

        public PageCommon_ModalWindow ContentHeight(int height)
        {
            _height = $"{height}px";

            return this;
        }

        public PageCommon_ModalWindow ContentWidth(string width)
        {
            _width = width;

            return this;
        }

        public PageCommon_ModalWindow ContentWidth(int width)
        {
            _width = $"{width}px";

            return this;
        }

        //public PageCommon_ModalWindow ContentWidth(string width)
        //{
        //    this.Style("width", width);

        //    return this;
        //}

        public override void Dispose()
        {
            ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

            ViewContext.Writer.WriteLine("</div> <!-- .modalcontent_inner -->");

            ViewContext.Writer.WriteLine("</div> <!-- .modalcontent -->");

            if (_leftButtons.HasData() || _rightButtons.HasData())
            {
                ViewContext.Writer.WriteLine("<div class=\"box-footer modalfooter\">");

                ViewContext.Writer.WriteLine("<div class=\"leftbuttonarea\">");

                foreach (var btn in _leftButtons)
                {
                    if (btn._hide)
                    {
                        continue;
                    }


                    btn.Css("btn-" + btn.GetColor);
                    btn.Command?.RenderToButton(btn);

                    ViewContext.Writer.WriteLine($"<button {btn.Attributes}>{btn._iconHtml}{btn.GetText}</button>");
                }

                ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

                ViewContext.Writer.WriteLine("</div>");



                ViewContext.Writer.WriteLine("<div class=\"rightbuttonarea\">");

                foreach (var btn in _rightButtons)
                {
                    if (btn._hide)
                    {
                        continue;
                    }

                    btn.Css("btn-" + btn.GetColor);
                    btn.Command?.RenderToButton(btn);

                    ViewContext.Writer.WriteLine($"<button {btn.Attributes}>{btn._iconHtml}{btn.GetText}</button>");
                }

                ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

                ViewContext.Writer.WriteLine("</div>");


                ViewContext.Writer.WriteLine("</div> <!-- .modalfooter -->");

                ViewContext.ViewData.Remove("ModalWindowID");
            }

            if (!AdminLTEUI.IsInPage) //如果是独立输出Modal的话,则输出提示文本
            {
                new PageCommon_ServerMsg(ViewContext).Render();
            }

            ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

            

            var validator = (IClientValidateRender)this.ViewContext.HttpContext.RequestServices.GetService(typeof(IClientValidateRender));

            if (validator!=null)
            {
                ViewContext.Writer.WriteLine("<script>");

                validator.Render(ViewContext);

                ViewContext.Writer.WriteLine("</script>");
            }
            

            ViewContext.Writer.WriteLine("</div> <!-- .modalwindow -->");

            ViewContext.Writer.WriteLine("<div class=\"clearfix\"></div>");

            AdminLTEUI.IsInModal = false;
        }

        internal override ContainerBase RenderInternal()
        {
            AdminLTEUI.IsInModal = true;

            var id = Attributes.GetValue("id","");

            if (string.IsNullOrWhiteSpace(id))
            {
                id = "modal" + ((AdminLTEUI) this.ViewContext.ViewBag.WebUI).NextControlIndex();

                Attributes.ID(id);
            }

            Attributes.Data("data-action", ViewContext.HttpContext.Request.GetDisplayUrl());

            Attributes.Data("data-shadeclose", _shadeClose.ToString());

            ViewContext.ViewData["ModalWindowID"] = id;

            ViewContext.Writer.WriteLine($"<div {Attributes}>");

            ViewContext.Writer.WriteLine("<div class=\"box-header with-border modaltitle\">");
            ViewContext.Writer.WriteLine($"<h4 class=\"box-title\"><i class=\"fa {_iconCss}\" style=\"margin-right: 10px\"></i>{_title}</h4>");
            ViewContext.Writer.WriteLine("</div> <!-- .modaltitle -->");

            ViewContext.Writer.WriteLine($"<div class=\"box-body modalcontent\" style=\"height:{_height};\">");

            ViewContext.Writer.WriteLine($"<div class=\"modalcontent_inner\" style=\"width:{_width};\">");

            return this;
        }
    }
}
